package dao;

import com.alibaba.fastjson.JSON;
import pojo.equip.EquipMsgEntity;
import pojo.product.ProductMsgEntity;
import utils.DataFileName;
import utils.DataUtils;
import utils.JsonUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Limohan
 */
public class EquipMsgDao {
    public void saveMsg(EquipMsgEntity msg) {
        String json = JSON.toJSONString(msg);
        try {
            DataUtils.writeData(DataFileName.EQUIPMENTMANAGER.toString(), json);
        } catch (NullPointerException | IOException e) {
            e.printStackTrace();
        }
    }

    public List<EquipMsgEntity> findMsgList() throws IOException {
        String json = DataUtils.readData(DataFileName.EQUIPMENTMANAGER.getName());
        if (json.length() == 0) {
            return null;
        }
        List<EquipMsgEntity> msgEntities = new ArrayList<>();
        String[] types = json.split("/");
        for (String str : types) {
            EquipMsgEntity msgEntity = JsonUtil.jsonToObject(str, EquipMsgEntity.class);
            msgEntities.add(msgEntity);
        }
        return msgEntities;
    }

    public List<EquipMsgEntity> findMsgByName(String name) throws IOException {
        List<EquipMsgEntity> equipMsgEntities = findMsgList();
        List<EquipMsgEntity> equipMsgEntities1 = new ArrayList<>();
        if (equipMsgEntities != null) {
            for (EquipMsgEntity equipMsgEntity : equipMsgEntities) {
                if (name.equals(equipMsgEntity.getName())) {
                    equipMsgEntities1.add(equipMsgEntity);
                }
            }
            return equipMsgEntities1;
        }
        return null;

    }

    public void changeType(String oldType, String newType) throws IOException {
        List<EquipMsgEntity> equipMsgEntities = findMsgList();
        if(equipMsgEntities==null) return;
        for (EquipMsgEntity equipMsgEntity : equipMsgEntities) {
            if (equipMsgEntity.getType().equals(oldType))
                equipMsgEntity.setType(newType);
        }
        DataUtils.clearData(DataFileName.EQUIPMENTMANAGER.toString());
        for (EquipMsgEntity msgEntity : equipMsgEntities) {
            this.saveMsg(msgEntity);
        }
    }

    public boolean findType(String type) throws Exception {
        List<EquipMsgEntity> equipMsgEntities = findMsgList();
        if (equipMsgEntities == null) return false;
        for (EquipMsgEntity msgEntity : equipMsgEntities) {
            if (msgEntity.getType().equals(type))
                return true;
        }
        return false;
    }

    public String getMaxId() throws IOException {
        List<EquipMsgEntity> equipMsgEntities = findMsgList();
        int max = 0;
        if (equipMsgEntities == null || equipMsgEntities.size() == 0) {
            return "1";
        } else {
            for (EquipMsgEntity equipMsgEntity : equipMsgEntities) {
                if (Integer.parseInt(equipMsgEntity.getId()) > max) {
                    max = Integer.parseInt(equipMsgEntity.getId());
                }
            }
            return String.valueOf(max + 1);
        }
    }

    public void deleteMsg(String id) throws IOException {
        List<EquipMsgEntity> equipMsgEntities = findMsgList();
        equipMsgEntities.removeIf(equipMsgEntity -> equipMsgEntity.getId().equals(id));
        DataUtils.clearData(DataFileName.EQUIPMENTMANAGER.toString());
        for (EquipMsgEntity equipMsgEntity : equipMsgEntities) {
            this.saveMsg(equipMsgEntity);
        }
    }

    public boolean isEquip(String type) throws IOException {
        EquipTypeDao equipTypeDao = new EquipTypeDao();
        return equipTypeDao.isType(type);
    }

    public EquipMsgEntity findMsg(String id) throws IOException {
        List<EquipMsgEntity> equipMsgEntityList = findMsgList();
        if (equipMsgEntityList != null) {
            for (EquipMsgEntity equipMsgEntity : equipMsgEntityList) {
                if (id.equals(equipMsgEntity.getId())) {
                    return equipMsgEntity;
                }
            }
        }
        return null;
    }

    public void changeRentState(EquipMsgEntity equipEntity) throws IOException {
        List<EquipMsgEntity> equipEntityList = findMsgList();
        if (equipEntity != null) {
            for (EquipMsgEntity equip : equipEntityList) {
                if (equip.getId().equals(equipEntity.getId())) {
                    if (equipEntity.getRentState().equals("未被租用")) {
                        equip.setRentState("已被租用");
                    } else {
                        equip.setRentState("未被租用");
                    }
                }
            }
            DataUtils.clearData(DataFileName.EQUIPMENTMANAGER.toString());
            for (EquipMsgEntity equip : equipEntityList) {
                this.saveMsg(equip);
            }
        }
    }

    public void changeEquipState(EquipMsgEntity equipMsgEntity) throws IOException {
        List<EquipMsgEntity> equipMsgEntityList = findMsgList();
        for (EquipMsgEntity equipMsg : equipMsgEntityList) {
            if (equipMsg.getId().equals(equipMsgEntity.getId())) {
                if (equipMsgEntity.getEquipState().equals("已关闭")) {
                    equipMsg.setEquipState("闲置中");
                } else {
                    equipMsg.setEquipState("已关闭");
                }
            }
        }
        DataUtils.clearData(DataFileName.EQUIPMENTMANAGER.toString());
        for (EquipMsgEntity equipMsg : equipMsgEntityList) {
            this.saveMsg(equipMsg);
        }

    }

    public List<EquipMsgEntity> findListByFactoryName(String factoryName) throws IOException {
        List<EquipMsgEntity> equipMsgEntityList = new ArrayList<>();
        if (this.findMsgList() != null) {
            for (EquipMsgEntity equip : this.findMsgList())
                if (equip.getFactory() != null && equip.getFactory().equals(factoryName))
                    equipMsgEntityList.add(equip);
            return equipMsgEntityList;
        }
        return null;
    }

    public List<EquipMsgEntity> findUnrentedList() throws IOException {
        List<EquipMsgEntity> equipMsgEntityList = new ArrayList<>();
        if (this.findMsgList() != null) {
            for (EquipMsgEntity equip : this.findMsgList()) {
                if (equip.getRentState().equals("未被租用"))
                    equipMsgEntityList.add(equip);
            }
            return equipMsgEntityList;
        }
        return null;
    }

    public void addProductMap(String id, String name, String efficiency) throws IOException {
        List<EquipMsgEntity> equipMsgEntityList = findMsgList();
        if(equipMsgEntityList==null)return;
        for(EquipMsgEntity equip:equipMsgEntityList){
            if (equip.getId().equals(id))
                equip.getProductMap().put(name, efficiency);
        }
        DataUtils.clearData(DataFileName.EQUIPMENTMANAGER.toString());
        for (EquipMsgEntity equipMsg :equipMsgEntityList ) {
            this.saveMsg(equipMsg);
        }

    }
}
